// src/router/index.js
import { createRouter, createWebHistory } from "vue-router";

// 导入组件（按需导入）
const index = () => import("@/views/index.vue");
const routes = [
  {
    path: "/",
    name: "index",
    component: index,
  }
];

const router = createRouter({
  history: createWebHistory(), // 使用 HTML5 History 模式
  routes,
});
// --在全局守卫中使用
router.beforeEach((to:any, from:any, next:any) => {
  console.log("全局守卫",from);
  if (to.meta.requiresAuth && !isLoggedIn()) {
    next("/login");
  } else {
    next();
  }
});
function isLoggedIn() {
  return !!localStorage.getItem("token"); // 检查是否存在 token
}
export default router;
